package cn.xmy.demo01.servlet;

import cn.xmy.demo01.pojo.Trolley;
import cn.xmy.demo01.service.TrolleyDaoService;
import cn.xmy.demo01.service.impl.TrolleyDaoImplService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;


import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/addTrolleyServlet")
public class AddTrolleyServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        TrolleyDaoService trolleyDaoService = new TrolleyDaoImplService();

        // 获取请求参数
        String trolleyName = req.getParameter("trolleyName");
        String number = req.getParameter("number");
        String price = req.getParameter("price");
        String total = req.getParameter("total");
        String img=req.getParameter("img");
        String ZhiId = req.getParameter("ZhiId");

        int rows=0;
        // 创建 Trolley 对象
        Trolley trolley = new Trolley(null, trolleyName, number, price, total,img, ZhiId);
        try {
            rows = trolleyDaoService.addTrolley(trolley);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        req.getRequestDispatcher("carImageServlet").forward(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
